package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.entity.SpuImage;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.entity.SpuSaleAttrValue;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.service.SpuSaleAttrValueService;
import com.atguigu.gmall.product.vo.SaveSpuVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.product.entity.SpuInfo;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.mapper.SpuInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author lfy
* @description 针对表【spu_info(商品表)】的数据库操作Service实现
* @createDate 2023-08-14 16:26:08
*/
@Service
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo>
    implements SpuInfoService{

    @Autowired
    SpuImageService spuImageService;

    @Autowired
    SpuSaleAttrService saleAttrService;


    @Autowired
    SpuSaleAttrValueService saleAttrValueService;


    @Transactional
    @Override
    public void saveSpuInfo(SaveSpuVo vo) {
        //1、把基础信息 保存到 spu_info
        SpuInfo info = new SpuInfo();
        BeanUtils.copyProperties(vo,info);
        save(info);

        //获取到刚才保存好的自增id
        Long spuId = info.getId();

        //2、把图片 保存到 spu_image
        List<SpuImage> spuImages = vo.getSpuImageList().stream()
                .map(item -> {
                    SpuImage spuImage = new SpuImage();
                    spuImage.setSpuId(spuId);
                    spuImage.setImgName(item.getImgName());
                    spuImage.setImgUrl(item.getImgUrl());
                    return spuImage;
                }).collect(Collectors.toList());

        spuImageService.saveBatch(spuImages);


        //3、把销售属性存到 spu_sale_attr
        List<SpuSaleAttr> collect = vo.getSpuSaleAttrList().stream()
                .map(item -> {
                    SpuSaleAttr saleAttr = new SpuSaleAttr();
                    saleAttr.setSpuId(spuId);
                    saleAttr.setBaseSaleAttrId(item.getBaseSaleAttrId());
                    saleAttr.setSaleAttrName(item.getSaleAttrName());

                    return saleAttr;
                }).collect(Collectors.toList());
        //  insert into xxx(a,b,c) values (1,1,1),(2,2,3)
        saleAttrService.saveBatch(collect);


        //4、把销售属性值存到 spu_sale_attr_value
        List<SpuSaleAttrValue> attrValues = vo.getSpuSaleAttrList().stream()
                .flatMap(item ->
                        item.getSpuSaleAttrValueList().stream().map(i->{
                            i.setSaleAttrName(item.getSaleAttrName());
                            return i;
                        })
                )
                .map(item -> {
                    SpuSaleAttrValue attrValue = new SpuSaleAttrValue();

                    attrValue.setSpuId(spuId);
                    attrValue.setBaseSaleAttrId(item.getBaseSaleAttrId());
                    attrValue.setSaleAttrValueName(item.getSaleAttrValueName());
                    //冗余保存
                    attrValue.setSaleAttrName(item.getSaleAttrName());
                    return attrValue;
                }).collect(Collectors.toList());

        saleAttrValueService.saveBatch(attrValues);
    }
}




